package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


import java.text.ParseException;
import java.util.List;

@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void deleteById(Integer id) {
        Integer count = userMapper.findCountById(id);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.CHECKGROUP_IS_QUOTED);
        }
        userMapper.deleteAssociation(id);
        userMapper.deleteById(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void add(Integer[] roleIds, User user) throws Exception {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        userMapper.add(user);
        userMapper.setUserAndRole(roleIds, user.getId());
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> list = userMapper.findByCondition(queryString);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userMapper.findRoleIdsByUserId(id);
    }

    @Override
    public void edit(Integer[] roleIds, User user) throws ParseException {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(encode);
       Integer count=userMapper.findByUserId(user.getId());
       if (count>0){
           userMapper.deleteUserAndRole(user.getId());
       }
        if (roleIds!=null&&roleIds.length>0){
            userMapper.setUserAndRole(roleIds, user.getId());
           String roleName=userMapper.findNameBy(user.getId());
           user.setRemark(roleName);
        }
        userMapper.edit(user);
    }

    @Override
    public User checkUsername(String username) {
        return userMapper.checkUsername(username);
    }

}